package com.ruoyi.paip.controller;

import com.ruoyi.paip.cache.PaipCache;
import com.ruoyi.paip.domain.paip.*;
import com.ruoyi.paip.factory.PaipClientFactory;
import com.ruoyi.paip.util.JsonUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 拍拍平台接口信息获取
 *
 * @author 辛凤文
 * @since 1.0
 */
@RestController
@RequestMapping("rush/paip")
public class PaipController {

    //@Resource
    //private PaipClient paipClient;

    //public Object test() {
    //    String res = paipClient.catchCondition();
    //    return res;
    //}

    @GetMapping("conditions")
    public Object allConditions() {
        return PaipCache.getConditionData();
    }

    @GetMapping("categories")
    public Object getCategories() {
        return PaipCache.getCategories();
    }

    @GetMapping("brands")
    public Object getBrands(Long cid3) {
        return PaipCache.getBrands(cid3);
    }

    @GetMapping("models")
    public Object getModels(Long cid3, String brandValue) {
        return PaipCache.getModels(cid3, brandValue);
    }

    @GetMapping("qualities")
    public Object getQualities() {
        return PaipCache.getQualities();
    }

    @GetMapping("productSources")
    public Object getProductSources() {
        return PaipCache.getProductSources();
    }

    @GetMapping("ranges")
    public Object getRanges() {
        return PaipCache.getRanges();
    }

    @GetMapping("sorts")
    public Object getSorts() {
        return PaipCache.getSorts();
    }

    @PostMapping("model/conditions")
    public Object getModelConditions(@RequestBody SearchFilter searchFilter) {
        String value = JsonUtils.toJsonAndEncode(searchFilter);
        Response<ModelConditionData> res = PaipClientFactory.getInstance().getCondition(value);
        if (res.isSuccess()) {
            Result<ModelConditionData> result = res.getResult();
            if (result.getSuccess()) {
                return result.getData();
            } else {
                throw new RuntimeException(result.getMsg());
            }
        }
        return res;
    }

    @PostMapping("search")
    public Object search(@RequestBody SearchFilter searchFilter) {
        String value = JsonUtils.toJsonAndEncode(searchFilter);
        Response<ProductSearchData> res = PaipClientFactory.getInstance().list(value);
        ProductSearchData data = res.getData();
        if (data != null && data.hasProduct()) {
            return data.getList();
        }
        return res;
    }

    @PostMapping("promo/batch")
    public Object getPromoBatch(@RequestBody PromoBatchForm form) {
        String value = JsonUtils.toJsonAndEncode(form);
        String res = PaipClientFactory.getInstance().promoBatch(value);
        //ProductSearchData data = res.getData();
        //if (data != null && data.hasProduct()) {
        //    return data.getList();
        //}
        return res;
    }

    @PostMapping("order/submit")
    public Object submitOrder(@RequestBody OrderInfo orderInfo) {
        String value = JsonUtils.toJsonAndEncode(orderInfo);
        return PaipClientFactory.getInstance().submitOrder(value);
    }

    public static void main(String[] args) {
        //testCatchCondition();
        //testGetCondition();
        String json = "{\"pageNo\":1,\"pageSize\":12,\"source\":1,\"conditionJson\":[{\"cType\":\"range\",\"fromValue\":\"\",\"toValue\":4850,\"code\":\"PRICE\"},{\"cType\":\"term\",\"code\":\"QUALITY\",\"value\":\"99\",\"name\":\"99新\"},{\"cType\":\"term\",\"code\":\"VERSION\",\"value\":\"16_169_国行\",\"name\":\"国行\"},{\"cType\":\"term\",\"code\":\"NETWORK\",\"value\":\"17_182_全网通\",\"name\":\"全网通\"},{\"name\":\"华为\",\"code\":\"BRAND\",\"value\":\"8557\",\"cType\":\"term\"},{\"name\":\"华为 Mate 60 Pro\",\"code\":\"MODEL\",\"value\":\"8295\",\"cType\":\"term\"},{\"cType\":\"term\",\"children\":null,\"code\":\"PRODUCT_SOURCE\",\"fromName\":null,\"fromValue\":null,\"name\":\"严选\",\"toName\":null,\"toValue\":null,\"value\":\"YX\"}],\"cid3\":13768}";
        //String bodyJson = "{\"orderChannel\":2,\"skuId\":10101347678146,\"inspectSkuId\":101093433938947,\"addrId\":10697899865,\"totalFee\":\"2789.00\",\"shippingFee\":\"0.00\",\"skuFee\":\"3209.00\",\"channelCode\":\"\",\"employeeId\":\"\",\"accountId\":\"\",\"additionalInfo\":{\"client\":\"\",\"unionId\":\"oCwKwuM1ukfsMfL3VidqpxxjV88U\",\"originProduct\":\"\",\"shareExtendInfo\":{\"salerInfo\":\"\",\"shareTime\":\"\"}},\"couponFee\":-420,\"selectedCouponIds\":[\"535712092973\"]}";
        String bodyJson = "{\"productSet\":[{\"skuId\":10092923798476,\"inspectSkuId\":100865704729635,\"type\":1}," +
                "{\"skuId\":10092923798476,\"inspectSkuId\":100866158250025,\"type\":1},{\"skuId\":10092923798476,\"inspectSkuId\":100471211576322,\"type\":1},{\"skuId\":10092923798476,\"inspectSkuId\":100786795190283,\"type\":1}],\"source\":1,\"channel\":7}";

        System.out.println(bodyJson);
    }

    public static void testCatchCondition() {
        Response<ConditionData> content = PaipClientFactory.getInstance().catchCondition();
        System.out.println(content);
    }

    public static void testGetCondition() {
        SearchFilter searchFilter = new SearchFilter();
        searchFilter.setCid3(13768L);
        List<Condition> conditionList = new ArrayList<>();

        // 创建第一个 Condition 对象并设置属性
        Condition condition1 = new Condition();
        condition1.setCType("range");
        condition1.setFromValue("0");
        condition1.setToValue("6000");
        condition1.setCode("PRICE");
        conditionList.add(condition1);

        // 创建第二个 Condition 对象并设置属性
        Condition condition2 = new Condition();
        condition2.setCType("term");
        condition2.setCode("QUALITY");
        condition2.setValue("99");
        condition2.setName("99新");
        conditionList.add(condition2);

        // 创建第三个 Condition 对象并设置属性
        Condition condition3 = new Condition();
        condition3.setCType("term");
        condition3.setCode("QUALITY");
        condition3.setValue("95");
        condition3.setName("95新");
        conditionList.add(condition3);

        // 创建第四个 Condition 对象并设置属性
        Condition condition4 = new Condition();
        condition4.setCType("term");
        condition4.setCode("COLOR");
        condition4.setValue("11_11_粉色");
        condition4.setName("粉色");
        conditionList.add(condition4);

        // 创建第五个 Condition 对象并设置属性
        Condition condition5 = new Condition();
        condition5.setCType("term");
        condition5.setCode("COLOR");
        condition5.setValue("11_2_绿色");
        condition5.setName("绿色");
        conditionList.add(condition5);

        // 创建第六个 Condition 对象并设置属性
        Condition condition6 = new Condition();
        condition6.setCType("term");
        condition6.setCode("COLOR");
        condition6.setValue("11_13_蓝色");
        condition6.setName("蓝色");
        conditionList.add(condition6);

        // 创建第七个 Condition 对象并设置属性
        Condition condition7 = new Condition();
        condition7.setCType("term");
        condition7.setCode("COLOR");
        condition7.setValue("11_4_黄色");
        condition7.setName("黄色");
        conditionList.add(condition7);

        // 创建第八个 Condition 对象并设置属性
        Condition condition8 = new Condition();
        condition8.setCType("term");
        condition8.setCode("COLOR");
        condition8.setValue("11_8_黑色");
        condition8.setName("黑色");
        conditionList.add(condition8);

        // 创建第九个 Condition 对象并设置属性
        Condition condition9 = new Condition();
        condition9.setCType("term");
        condition9.setCode("MEMORY");
        condition9.setValue("14_16_512G");
        condition9.setName("512G");
        conditionList.add(condition9);

        // 创建第十个 Condition 对象并设置属性
        Condition condition10 = new Condition();
        condition10.setCType("term");
        condition10.setCode("MEMORY");
        condition10.setValue("14_23_256G");
        condition10.setName("256G");
        conditionList.add(condition10);

        // 创建第十一个 Condition 对象并设置属性
        Condition condition11 = new Condition();
        condition11.setCType("term");
        condition11.setCode("MEMORY");
        condition11.setValue("14_15_128G");
        condition11.setName("128G");
        conditionList.add(condition11);

        // 创建第十二个 Condition 对象并设置属性
        Condition condition12 = new Condition();
        condition12.setCType("term");
        condition12.setCode("VERSION");
        condition12.setValue("16_169_国行");
        condition12.setName("国行");
        conditionList.add(condition12);

        // 创建第十三个 Condition 对象并设置属性
        Condition condition13 = new Condition();
        condition13.setCType("term");
        condition13.setCode("VERSION");
        condition13.setValue("16_5040_其他地区");
        condition13.setName("其他地区");
        conditionList.add(condition13);

        // 创建第十四个 Condition 对象并设置属性
        Condition condition14 = new Condition();
        condition14.setCType("term");
        condition14.setCode("NETWORK");
        condition14.setValue("17_182_全网通");
        condition14.setName("全网通");
        conditionList.add(condition14);

        // 创建第十五个 Condition 对象并设置属性
        Condition condition15 = new Condition();
        condition15.setCType("term");
        condition15.setCode("BRAND");
        condition15.setValue("14026");
        condition15.setName("苹果");
        conditionList.add(condition15);

        // 创建第十六个 Condition 对象并设置属性
        Condition condition16 = new Condition();
        condition16.setCType("term");
        condition16.setCode("MODEL");
        condition16.setValue("8356");
        condition16.setName("苹果 iPhone 15 Plus");
        conditionList.add(condition16);

        // 创建第十七个 Condition 对象并设置属性
        Condition condition17 = new Condition();
        condition17.setCType("term");
        condition17.setCode("PRODUCT_SOURCE");
        condition17.setName("严选");
        condition17.setValue("YX");
        conditionList.add(condition17);

        searchFilter.setConditionJson(conditionList);
        String value = JsonUtils.toJsonAndEncode(searchFilter);
        Response<ModelConditionData> res = PaipClientFactory.getInstance().getCondition(value);
        System.out.println(res);
    }


}
